package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.user.dto.ApAuthor;
import com.heima.user.dto.AuthDto;
import com.heima.user.dto.WmUser;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.feign.ArticleFeign;
import com.heima.user.feign.MediaFeign;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.heima.user.service.IApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author wsj
 * @since 2022-03-11
 */
@Service
@GlobalTransactional
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {

    @Override
    public ResponseResult listByStatus(AuthDto dto) {
        //构建分页条件
        IPage<ApUserRealname> page=new Page<>(dto.getPage(),dto.getSize());
        //判断状态是否存在，存在则构建查询条件
        LambdaQueryWrapper<ApUserRealname> query=new LambdaQueryWrapper<>();
        if (dto.getStatus()!=null) {
            query.eq(ApUserRealname::getStatus,dto.getStatus());
        }
        //分页查询
        IPage<ApUserRealname> iPage = page(page, query);
        //返回分页结果
        return new PageResponseResult(dto.getPage(), dto.getSize(), iPage.getTotal(), iPage.getRecords());
    }
    @Autowired
    private IApUserService apUserService;
    @Autowired
    private MediaFeign mediaFeign;
    @Autowired
    private ArticleFeign articleFeign;
    @Override
    public ResponseResult authPass(AuthDto dto, int i) {
        //判断是否审核成功
        ApUserRealname apUserRealname = getById(dto.getId());
        //审核失败
        if (i==0) {
            //根据id更改审核状态
            apUserRealname.setStatus(2);
            apUserRealname.setReason(dto.getReason());
            updateById(apUserRealname);
            return ResponseResult.okResult();
        }
        //审核成功
        //更新用户状态为自媒体
        ApUser apUser = apUserService.getById(apUserRealname.getUserId());
        apUser.setFlag(1);
        apUserService.updateById(apUser);
        //更改审核状态
        apUserRealname.setStatus(9);
        updateById(apUserRealname);
        //新增自媒体账号
        WmUser wmUser = createWmUser(apUser);
        if (wmUser!=null) {
            //新增文章作者
           ApAuthor apAuthor= createApAuthor(apUser,wmUser.getId());
           //模拟错误
           //int a=1/0;
           //设置自媒体文章作者id
            if (apAuthor!=null) {
                wmUser.setApAuthorId(apAuthor.getId());
                mediaFeign.update(wmUser);
            }
        }
        return ResponseResult.okResult();
    }

    private ApAuthor createApAuthor(ApUser apUser, Integer id) {
        ApAuthor apAuthor=new ApAuthor();
        apAuthor.setName(apUser.getName());
        apAuthor.setType(2);
        apAuthor.setUserId(apUser.getId());
        apAuthor.setWmUserId(id);
        ResponseResult<ApAuthor> apAuthorResponseResult = articleFeign.save(apAuthor);
        if (apAuthorResponseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            ApAuthor author = apAuthorResponseResult.getData();
            return author;
        }
        return null;
    }

    public WmUser createWmUser(ApUser apUser) {
        WmUser wmUser = new WmUser();
        BeanUtils.copyProperties(apUser,wmUser);
        wmUser.setId(null);
        wmUser.setApUserId(apUser.getId());
        wmUser.setNickname(apUser.getName());
        wmUser.setStatus(9);
        wmUser.setType(0);
        ResponseResult<WmUser> wmUserResponseResult = mediaFeign.save(wmUser);
        if (wmUserResponseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            WmUser wmUser1 = wmUserResponseResult.getData();
            return wmUser1;
        }
        return null;
    }
}
